package org.liveboardgames.agricola.control.delegates.action.prerequisite
{
    import mx.collections.ListCollectionView;
    
    import org.liveboardgames.agricola.vo.farm.FarmElement;
    import org.liveboardgames.agricola.vo.farm.FarmTile;
    import org.liveboardgames.agricola.vo.player.Player;
    import org.liveboardgames.common.dictionary.collections.IMap;
    
    public class FreeFieldPrerequisiteChecker extends PrerequisiteChecker
    {
        public function FreeFieldPrerequisiteChecker(player:Player)
        {
            super(player);
        }
        
        override public function check():Boolean
        {
            var result:Boolean = false;
            
            var farmTiles:ListCollectionView = player.farm.tiles;
            for each (var tile:FarmTile in farmTiles)
            {
                // If it is a field...
                var construction:String = tile.farmElement;
                if (construction == FarmElement.FIELD)
                {
                    // Check whether it is free
                    var resources:IMap = tile.resources;
                    if (resources == null || resources.getKeys().length == 0)
                    {
                        result = true;
                    }
                }
            }
            
            return result;
        }
    
    }
}